/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-20
 * Time: 11:26
 */
public class pivot_Index {
    public static int pivotIndex(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            dp[i] = dp[i - 1] + nums[i - 1];
        }

        int ret = -1;

        for (int i = 1; i < n + 1; i++) {
            if (dp[i - 1] == dp[n] - dp[i]) {
                ret = i;
                break;
            }
        }
        return ret == -1 ? -1 : ret - 1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 7, 3, 6, 5, 6};
        System.out.println(pivotIndex(arr));
    }
}
